package com.jsgt.greekfire.ui.me.backup

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.fhx.core.goToActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.base.BaseBackgroundActivity
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.ui.home.entrance.createwallet.checkhelpword.CheckHelpWordActivity
import com.jsgt.greekfire.ui.mine.user.backupwalletword.BackUpWalletWordActivity
import kotlinx.android.synthetic.main.activity_backup.*

class BackupActivity : BaseBackgroundActivity(), View.OnClickListener {

    private val tipsDialog: BackupTipsDialog by lazy {
        BackupTipsDialog().okListener {
            if (identity == null) {
                launcher?.launch(
                    Intent(this, CheckHelpWordActivity::class.java).putExtra(
                        "identityId",
                        identityId
                    )
                )
            } else {
                launcher?.launch(
                    Intent(this, CheckHelpWordActivity::class.java).putExtra(
                        "identity",
                        identity
                    )
                )
            }
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
        }
    }

    private var launcher: ActivityResultLauncher<Intent>? = null
    private var identityId = -1
    private var identity: IdentityBean? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_backup
    }

    override fun main() {
        identityId = intent.getIntExtra("identityId", -1)
        identity = intent.getParcelableExtra("identity")
        if (identityId == -1 && identity == null) {
            finish()
        }

        if (identity == null) {
            back.setOnClickListener(this)
            txvBackupNow.setOnClickListener(this)
            txvBackupLater.setOnClickListener(this)
        } else {
            txvBackupNow.setOnClickListener(this)
            txvBackupLater.visibility = View.INVISIBLE
            txvBackupNow.text = getString(R.string.next_step)
        }

        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Activity.RESULT_OK) {
                if (identity == null) {
                    goToActivity<BackUpWalletWordActivity> {
                        putExtra("identityId", identityId)
                    }
                    finish()
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
                } else {
                    setResult(RESULT_OK)
                    finish()
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.back -> {
                onBackPressed()
            }
            R.id.txvBackupNow -> {
//                showInputPwdDialog()
                tipsDialog.show(supportFragmentManager, "tips")
            }
            R.id.txvBackupLater -> {
                onBackPressed()
            }
        }
    }
}